package com.wxtwxt.ruralmall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.wxtwxt.ruralmall.product.config.MyThreadConfig;
import com.wxtwxt.ruralmall.product.entity.SkuImagesEntity;
import com.wxtwxt.ruralmall.product.entity.SpuInfoDescEntity;
import com.wxtwxt.ruralmall.product.service.*;
import com.wxtwxt.ruralmall.product.vo.SkuItemVo;
import com.wxtwxt.ruralmall.product.vo.SkuSaleAttrVo;
import com.wxtwxt.ruralmall.product.vo.SpuBaseAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxtwxt.ruralmall.common.utils.PageUtils;
import com.wxtwxt.ruralmall.common.utils.Query;

import com.wxtwxt.ruralmall.product.dao.SkuInfoDao;
import com.wxtwxt.ruralmall.product.entity.SkuInfoEntity;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Resource
    private SkuImagesService skuImagesService;
    @Resource
    private SpuInfoDescService spuInfoDescService;
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String catelogId = (String) params.get("catelogId");
        if(StringUtils.isNotEmpty(catelogId) && !"0".equals(catelogId)) {
            queryWrapper.eq("catalog_id", catelogId);
        }
        String brandId = (String) params.get("brandId");
        if(StringUtils.isNotEmpty(brandId) && !"0".equals(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        BigDecimal small = new BigDecimal(min);
        queryWrapper.ge("price", small);
        BigDecimal big = new BigDecimal(max);
        if(big.compareTo(new BigDecimal(0)) > 0) {
            queryWrapper.le("price", big);
        }
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)) {
            queryWrapper.and(wrapper -> wrapper.eq("sku_id", key).or().like("sku_name", key));
        }
        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        if(spuId != null) {
            return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        }
        return null;
    }

    @Override
    public SkuItemVo getSkuItem(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        CompletableFuture<SkuInfoEntity> skuInfo = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = this.getById(skuId);
            skuItemVo.setSkuInfoEntity(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);
        CompletableFuture<Void> skuImagesEntitiesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImagesEntities = skuImagesService.getImageBySkuId(skuId);
            skuItemVo.setImages(skuImagesEntities);
        }, threadPoolExecutor);
        CompletableFuture<Void> spuInfoDescFuture = skuInfo.thenAcceptAsync((result) -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getSpuInfoDescBySpuId(result.getSpuId());
            skuItemVo.setSpuInfoDescEntity(spuInfoDescEntity);
        }, threadPoolExecutor);
        CompletableFuture<Void> groupAttrsFuture = skuInfo.thenAcceptAsync((result) -> {
            List<SpuBaseAttrGroupVo> spuBaseAttrGroupVoList = attrGroupService.getBaseAttrGroupVo(result.getCatalogId(), result.getSpuId());
            skuItemVo.setGroupAttrs(spuBaseAttrGroupVoList);
        }, threadPoolExecutor);
        CompletableFuture<Void> saleAttrFuture = skuInfo.thenAcceptAsync((result) -> {
            List<SkuSaleAttrVo> skuSaleAttrVoList = skuSaleAttrValueService.getSaleAttrVo(result.getSpuId());
            skuItemVo.setSaleAttr(skuSaleAttrVoList);
        }, threadPoolExecutor);
        try {
            CompletableFuture.allOf(skuImagesEntitiesFuture, spuInfoDescFuture, groupAttrsFuture, saleAttrFuture).get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return skuItemVo;
    }

}